package phoenics.manpads.stinger.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import phoenics.manpads.common.mybatis.page.PaginationInterceptor;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @author Phoenics Chow
 * on 2019年01月19日
 */
@Configuration
@EnableTransactionManagement
public class DataBaseConfig {
    @Bean(name = "primaryDataSource") // 声明其为Bean实例
    @Qualifier("primaryDataSource")
    @Primary // 在同样的DataSource中，首先使用被标注的DataSource
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource mainDataSource() {
        DruidDataSource datasource = new DruidDataSource();
        return datasource;
    }

    // 创建事务管理器1
    @Bean
    public PlatformTransactionManager txManager(DataSource dataSource) throws SQLException {
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor=new PaginationInterceptor();
        paginationInterceptor.setDialect("mysql");
        return paginationInterceptor;
    }

}
